package solutions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.pi4.locutil.GeoPosition;
import org.pi4.locutil.MACAddress;
import org.pi4.locutil.io.TraceGenerator;
import org.pi4.locutil.trace.TraceEntry;

public class ModelNN extends BasePositioner {
	public final static int METERS_IN_Y = 30;
	public final static int METERS_IN_X = 57;
	private static final double d_0 = 1;
	private static final double P_d0 = -33.77;
	private static final double N = 3.415;

	public static double GRID_SIZE = 1.5;

	public static int NR_GRIDS_X = (int) (METERS_IN_X / GRID_SIZE);
	public static int NR_GRIDS_Y = (int) (METERS_IN_Y / GRID_SIZE);
	List<EntryWithMACsAndAverages> radioMap = new ArrayList<EntryWithMACsAndAverages>();
	private List<TraceEntry> apPositions;

	public ModelNN(TraceGenerator tg, List<Double> differences,
			List<TraceEntry> apPositions) {
		this.tg = tg;
		this.differences = differences;
		this.apPositions = apPositions;
	}

	public void buildRadioMap(List<TraceEntry> onlineTraces) {
		double x = 31.5, y = -18;

		for (int i = 0; i < NR_GRIDS_X; i++) {
			y = -18;
			for (int j = 0; j < NR_GRIDS_Y; j++) {
				GeoPosition gridPosition = new GeoPosition(x, y);

				Map<MACAddress, Double> wifiPositionsStrengths = new HashMap<MACAddress, Double>();

				for (TraceEntry entry : apPositions) {
					double distance = PositionUtils.getDistance(
							entry.getGeoPosition(), gridPosition);
					double P_d = P_d0 - 10 * N * Math.log10(distance / d_0);
					if (P_d <= 90)
						wifiPositionsStrengths.put(entry.getId(), P_d);
				}
				radioMap.add(new EntryWithMACsAndAverages(gridPosition,
						wifiPositionsStrengths));
				y += GRID_SIZE;
			}
			x -= GRID_SIZE;
		}
	}
	
	public List<EntryWithMACsAndAverages> getRadioMap() {
		return radioMap;
	}
}
